﻿<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Python复习</title>
  <link rel="stylesheet" href="https://stackedit.cn/style.css" />
</head>

<body class="stackedit">
  <div class="stackedit__html"><h1 id="python复习"><span class="prefix"></span><span class="content">Python复习</span><span class="suffix"></span></h1>
<h2 id="数据类型"><span class="prefix"></span><span class="content">数据类型</span><span class="suffix"></span></h2>

<table>
<thead>
<tr>
<th>名称</th>
<th>标识符</th>
<th>表示示例</th>
<th>构造函数</th>
<th>运算示例</th>
</tr>
</thead>
<tbody>
<tr>
<td>字符串</td>
<td>str</td>
<td><code>"a"</code><br><code>'a'</code><br><code>"""a"""</code><br><code>'''a'''</code></td>
<td><code>str(3.14)</code>-&gt;<code>"3.14"</code></td>
<td><code>"a"+"b"</code>-&gt;<code>ab</code></td>
</tr>
<tr>
<td>整数</td>
<td>int</td>
<td><code>0</code></td>
<td><code>int("1")</code>-&gt;<code>1</code><br><code>int(1.9)</code>-&gt;<code>1</code></td>
<td><code>1+2</code>-&gt;<code>3</code>(加)<br><code>9-10</code>-&gt;<code>-1</code>(减)<br><code>2*3</code>-&gt;<code>6</code>(乘)<br><code>1/2</code>-&gt;<code>0.5</code>(除)<br><code>2**3</code>-&gt;<code>8</code>(幂)<br><code>10//3</code>-&gt;<code>3</code>(整除)<br><code>10%3</code>-&gt;<code>1</code>(取模)</td>
</tr>
<tr>
<td>浮点数</td>
<td>float</td>
<td><code>3.14</code></td>
<td><code>float("1.5")</code>-&gt;<code>1.5</code><br><code>float(1)</code>-&gt;<code>1.0</code></td>
<td>与int相似</td>
</tr>
<tr>
<td>布尔值</td>
<td>bool</td>
<td><code>True</code><br><code>False</code></td>
<td><code>bool(0)</code>-&gt;<code>False</code></td>
<td><code>True and True</code>-&gt;<code>True</code><br><code>True and False</code>-&gt;<code>False</code><br><code>False and True</code>-&gt;<code>False</code><br><code>False and False</code>-&gt;<code>False</code><br><br><code>True or True</code>-&gt;<code>True</code><br><code>True or False</code>-&gt;<code>True</code><br><code>False or True</code>-&gt;<code>True</code><br><code>False or False</code>-&gt;<code>False</code><br><br><code>not True</code>-&gt;<code>False</code><br><code>not False</code>-&gt;<code>True</code></td>
</tr>
<tr>
<td>空值</td>
<td>——</td>
<td><code>None</code></td>
<td>——</td>
<td>——</td>
</tr>
<tr>
<td>列表</td>
<td>list</td>
<td><code>[1,10,5,"a"]</code></td>
<td><code>list()</code>-&gt;<code>[]</code></td>
<td>——</td>
</tr>
</tbody>
</table><h2 id="基本函数"><span class="prefix"></span><span class="content">基本函数</span><span class="suffix"></span></h2>

<table>
<thead>
<tr>
<th>标识符</th>
<th>功能</th>
<th>参数</th>
<th>返回值</th>
<th>示例</th>
</tr>
</thead>
<tbody>
<tr>
<td>print</td>
<td>输出参数序列</td>
<td>输出序列:<code>str</code></td>
<td><code>None</code></td>
<td><code>print("Hello!")</code></td>
</tr>
<tr>
<td>input</td>
<td>从键盘读取输入</td>
<td>提示信息:<code>str</code></td>
<td>键盘字符输入:<code>str</code></td>
<td><code>input("输入一个数字:")</code></td>
</tr>
<tr>
<td>chr</td>
<td>构造字符</td>
<td>字符编码:<code>int</code></td>
<td>编码对应字符:<code>str</code></td>
<td><code>chr(88)</code></td>
</tr>
<tr>
<td>input</td>
<td>获取参数字符对应编码</td>
<td>字符:<code>str</code></td>
<td>字符对应编码:<code>int</code></td>
<td><code>ord("X")</code></td>
</tr>
</tbody>
</table><h2 id="变量赋值"><span class="prefix"></span><span class="content">变量赋值</span><span class="suffix"></span></h2>
<h3 id="变量语法"><span class="prefix"></span><span class="content">变量语法</span><span class="suffix"></span></h3>
<pre class=" language-python"><code class="prism  language-python">变量 <span class="token operator">=</span> 值
变量序列 <span class="token operator">=</span> 值序列
</code></pre>
<p>示例：</p>
<pre class=" language-python"><code class="prism  language-python">a <span class="token operator">=</span> <span class="token number">1234567890</span>
x<span class="token punctuation">,</span> y<span class="token punctuation">,</span> z <span class="token operator">=</span> <span class="token string">"a"</span><span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token punctuation">,</span> <span class="token number">3.14</span>
</code></pre>
<h3 id="变量命名规则"><span class="prefix"></span><span class="content">变量命名规则</span><span class="suffix"></span></h3>
<ol>
<li>只包含数字、大小写字母、下划线（_）</li>
<li>不以数字开头</li>
<li>不与保留字（见保留字表）完全相同</li>
</ol>
<h3 id="保留字表"><span class="prefix"></span><span class="content">保留字表</span><span class="suffix"></span></h3>

<table>
<thead>
<tr>
<th>False</th>
<th>await</th>
<th>else</th>
<th>import</th>
<th>pass</th>
</tr>
</thead>
<tbody>
<tr>
<td>None</td>
<td>break</td>
<td>except</td>
<td>in</td>
<td>raise</td>
</tr>
<tr>
<td>True</td>
<td>class</td>
<td>finally</td>
<td>is</td>
<td>return</td>
</tr>
<tr>
<td>and</td>
<td>continue</td>
<td>for</td>
<td>lambda</td>
<td>try</td>
</tr>
<tr>
<td>as</td>
<td>def</td>
<td>from</td>
<td>nonlocal</td>
<td>while</td>
</tr>
<tr>
<td>assert</td>
<td>del</td>
<td>global</td>
<td>not</td>
<td>with</td>
</tr>
<tr>
<td>async</td>
<td>elif</td>
<td>if</td>
<td>or</td>
<td>yield</td>
</tr>
</tbody>
</table><h2 id="流程控制"><span class="prefix"></span><span class="content">流程控制</span><span class="suffix"></span></h2>
<blockquote>
<p>所有流程控制均包含语句块。语句块必须使用<strong>缩进</strong>以标明其隶属流程。</p>
</blockquote>
<h3 id="if分支"><span class="prefix"></span><span class="content">if分支</span><span class="suffix"></span></h3>
<pre class=" language-python"><code class="prism  language-python"><span class="token keyword">if</span> <span class="token operator">&lt;</span>条件<span class="token operator">&gt;</span><span class="token punctuation">:</span>
	<span class="token comment"># 分支1</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">elif</span> <span class="token operator">&lt;</span>条件<span class="token operator">&gt;</span><span class="token punctuation">:</span>
	<span class="token comment"># 分支2</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">elif</span> <span class="token operator">&lt;</span>条件<span class="token operator">&gt;</span><span class="token punctuation">:</span>
	<span class="token comment"># 分支n-1</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
	<span class="token comment"># 分支n</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre>
<ol>
<li>语法: 必选一个<code>if</code>引导的分支，可不选或选任意个<code>elif</code>引导的分支，可不选或选一个<code>else</code>引导的分支。</li>
<li>运行逻辑: Python判断表达式<code>&lt;条件&gt;</code>的值是否等价于<code>True</code>。若等价于<code>True</code>，则进入对应分支语句块。否则不会。若存在<code>else</code>引导的分支语句块，且此前的其<code>if</code>引导的所有分支语句块皆未进入，则无条件进入该<code>else</code>引导的分支语句块。</li>
</ol>
<h3 id="while循环"><span class="prefix"></span><span class="content">while循环</span><span class="suffix"></span></h3>
<pre class=" language-python"><code class="prism  language-python"><span class="token keyword">while</span> <span class="token operator">&lt;</span>条件<span class="token operator">&gt;</span><span class="token punctuation">:</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre>
<ol>
<li>语法: 必选一个<code>while</code>引导的循环。</li>
<li>运行逻辑: Python判断表达式<code>&lt;条件&gt;</code>的值是否等价于<code>True</code>。若等价于<code>True</code>，则进入对应循环语句块。否则不会。循环语句块运行结束时，再次判断。如此，循环往复。</li>
</ol>
<h3 id="for循环"><span class="prefix"></span><span class="content">for循环</span><span class="suffix"></span></h3>
<pre class=" language-python"><code class="prism  language-python"><span class="token keyword">for</span> <span class="token punctuation">[</span>循环变量<span class="token punctuation">]</span> <span class="token keyword">in</span> <span class="token punctuation">[</span>迭代器<span class="token punctuation">]</span><span class="token punctuation">:</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre>
<ol>
<li>语法: 必选一个<code>for</code>引导的循环。</li>
<li>运行逻辑: Python判断循环变量是否可以取值。若可以，则进入对应循环语句块。否则不会。循环语句块运行结束时，再次判断表达式<code>&lt;条件&gt;</code>的值是否等价于<code>True</code>循环变量是否可以取值。如此，循环往复。</li>
<li>常见迭代器：str、list、range</li>
</ol>
<h3 id="for-in-range结构"><span class="prefix"></span><span class="content">for-in range结构</span><span class="suffix"></span></h3>
<pre class=" language-python"><code class="prism  language-python"><span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">)</span><span class="token punctuation">:</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
</code></pre>
<p>此处循环变量<code>i</code>的取值遵循<span class="katex--inline"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo fence="true">{</mo><mtable rowspacing="0.25em" columnalign="right left" columnspacing="0em"><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mn>0</mn><mo>≤</mo><mi>n</mi><mo>&lt;</mo><mfrac><mrow><mi>b</mi><mo>−</mo><mi>a</mi></mrow><mi>c</mi></mfrac><mo separator="true">,</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mi>n</mi><mo>∈</mo><mi mathvariant="double-struck">N</mi><mo separator="true">,</mo></mrow></mstyle></mtd></mtr><mtr><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow></mrow></mstyle></mtd><mtd><mstyle scriptlevel="0" displaystyle="true"><mrow><mrow></mrow><mi>i</mi><mo>=</mo><mi>a</mi><mo>⋅</mo><mi>n</mi><mo>+</mo><mi>c</mi></mrow></mstyle></mtd></mtr></mtable></mrow><annotation encoding="application/x-tex">\left\{
\begin{aligned}
&amp;0 \leq n &lt; \frac{b - a}{c}, \\
&amp;n \in \mathbb{N}, \\
&amp;i = a \cdot n + c
\end{aligned}
\right.</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 5.4em; vertical-align: -2.45em;"></span><span class="minner"><span class="mopen"><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 2.95em;"><span class="" style="top: -1.6em;"><span class="pstrut" style="height: 3.15em;"></span><span class="delimsizinginner delim-size4"><span class="">⎩</span></span></span><span class="" style="top: -1.592em;"><span class="pstrut" style="height: 3.15em;"></span><span class="" style="height: 0.916em; width: 0.8889em;"><svg width="0.8889em" height="0.916em" style="width:0.8889em" viewBox="0 0 888.89 916" preserveAspectRatio="xMinYMin"><path d="M384 0 H504 V916 H384z M384 0 H504 V916 H384z"></path></svg></span></span><span class="" style="top: -3.15em;"><span class="pstrut" style="height: 3.15em;"></span><span class="delimsizinginner delim-size4"><span class="">⎨</span></span></span><span class="" style="top: -4.292em;"><span class="pstrut" style="height: 3.15em;"></span><span class="" style="height: 0.916em; width: 0.8889em;"><svg width="0.8889em" height="0.916em" style="width:0.8889em" viewBox="0 0 888.89 916" preserveAspectRatio="xMinYMin"><path d="M384 0 H504 V916 H384z M384 0 H504 V916 H384z"></path></svg></span></span><span class="" style="top: -5.2em;"><span class="pstrut" style="height: 3.15em;"></span><span class="delimsizinginner delim-size4"><span class="">⎧</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height: 2.45em;"><span class=""></span></span></span></span></span></span><span class="mord"><span class="mtable"><span class="col-align-r"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 2.9287em;"><span class="" style="top: -4.9287em;"><span class="pstrut" style="height: 3.3714em;"></span><span class="mord"></span></span><span class="" style="top: -3.1027em;"><span class="pstrut" style="height: 3.3714em;"></span><span class="mord"></span></span><span class="" style="top: -1.6027em;"><span class="pstrut" style="height: 3.3714em;"></span><span class="mord"></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height: 2.4287em;"><span class=""></span></span></span></span></span><span class="col-align-l"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 2.9287em;"><span class="" style="top: -4.9287em;"><span class="pstrut" style="height: 3.3714em;"></span><span class="mord"><span class="mord"></span><span class="mord">0</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mrel">≤</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mrel">&lt;</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height: 1.3714em;"><span class="" style="top: -2.314em;"><span class="pstrut" style="height: 3em;"></span><span class="mord"><span class="mord mathnormal">c</span></span></span><span class="" style="top: -3.23em;"><span class="pstrut" style="height: 3em;"></span><span class="frac-line" style="border-bottom-width: 0.04em;"></span></span><span class="" style="top: -3.677em;"><span class="pstrut" style="height: 3em;"></span><span class="mord"><span class="mord mathnormal">b</span><span class="mspace" style="margin-right: 0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right: 0.2222em;"></span><span class="mord mathnormal">a</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height: 0.686em;"><span class=""></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mpunct">,</span></span></span><span class="" style="top: -3.1027em;"><span class="pstrut" style="height: 3.3714em;"></span><span class="mord"><span class="mord"></span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mrel">∈</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mord mathbb">N</span><span class="mpunct">,</span></span></span><span class="" style="top: -1.6027em;"><span class="pstrut" style="height: 3.3714em;"></span><span class="mord"><span class="mord"></span><span class="mord mathnormal">i</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right: 0.2778em;"></span><span class="mord mathnormal">a</span><span class="mspace" style="margin-right: 0.2222em;"></span><span class="mbin">⋅</span><span class="mspace" style="margin-right: 0.2222em;"></span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right: 0.2222em;"></span><span class="mbin">+</span><span class="mspace" style="margin-right: 0.2222em;"></span><span class="mord mathnormal">c</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height: 2.4287em;"><span class=""></span></span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span></span></p>
<h3 id="def函数定义"><span class="prefix"></span><span class="content">def函数定义</span><span class="suffix"></span></h3>
<pre class=" language-python"><code class="prism  language-python"><span class="token keyword">def</span> <span class="token punctuation">(</span><span class="token punctuation">[</span>函数参数<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
	<span class="token keyword">return</span> <span class="token punctuation">[</span>返回值<span class="token punctuation">]</span>

<span class="token punctuation">[</span>函数<span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token punctuation">[</span>参数<span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre>
<ol>
<li>语法：必选一个<code>def</code>引导的函数定义。</li>
<li>运行逻辑：调用函数时，Python为参数赋值，进入函数语句块。若运行到<code>return [返回值]</code>，则函数的返回值为表达式<code>[返回值]</code>，并跳转至调用处。</li>
</ol>
</div>
</body>

</html>
